package org.kabeja.dxf.objects;

import org.kabeja.dxf.DXFConstants;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/**
 * @author shutao
 * @since 2022/4/24 16:48
 */
public class DXFDictionary extends DXFObject {
    protected List<Object> records = new ArrayList<>();

    public DXFDictionary() {
    }

    @Override
    public String getObjectType() {
        return DXFConstants.OBJECT_TYPE_DICTIONARY;
    }

    public boolean hasDXFObjectByID(String var1) {
        return this.findByID(var1) != null;
    }

    public String getNameForDXFObjectID(String var1) {
        return this.findByID(var1).getName();
    }

    public DXFObject getDXFObjectByID(String var1) {
        DXFDictionary var2 = this.getDXFDictionaryForID(var1);
        if (var2 != null) {
            DXFDictionary.DXFDictionaryRecord var3 = var2.findByID(var1);
            if (var3 != null) {
                return var3.getDXFObject();
            }
        }

        return null;
    }

    public DXFObject getDXFObjectByName(String var1) {
        DXFDictionary.DXFDictionaryRecord var2 = this.findByName(var1);
        return var2 != null ? var2.getDXFObject() : null;
    }

    public void putDXFObject(DXFObject var1) {
        this.findByID(var1.getID()).setDXFObject(var1);
    }

    public void putDXFObjectRelation(String var1, String var2) {
        DXFDictionary.DXFDictionaryRecord var3 = null;
        if ((var3 = this.findByName(var1)) != null) {
            var3.setID(var2);
        } else {
            var3 = new DXFDictionaryRecord(var1, var2);
            this.records.add(var3);
        }

    }

    protected DXFDictionary.DXFDictionaryRecord findByName(String var1) {
        for (Object record : this.records) {
            DXFDictionaryRecord var3 = (DXFDictionaryRecord) record;
            if (var3.getName().equals(var1)) {
                return var3;
            }
        }

        return null;
    }

    protected DXFDictionary.DXFDictionaryRecord findByID(String var1) {
        for (Object record : this.records) {
            DXFDictionaryRecord var3 = (DXFDictionaryRecord) record;
            if (var3.getID().equals(var1)) {
                return var3;
            }
        }

        return null;
    }

    public DXFDictionary getDXFDictionaryForID(String var1) {
        HashSet var2 = new HashSet();
        DXFObject var3 = null;

        for (Object record : this.records) {
            DXFDictionaryRecord var5 = (DXFDictionaryRecord) record;
            if (var5.getID().equals(var1)) {
                return this;
            }

            if ((var3 = var5.getDXFObject()) != null && "DICTIONARY".equals(var3.getObjectType())) {
                var2.add(var3);
            }
        }

        Iterator var7 = var2.iterator();

        DXFDictionary var6;
        do {
            if (!var7.hasNext()) {
                return null;
            }

            DXFDictionary var8 = (DXFDictionary)var7.next();
            var6 = var8.getDXFDictionaryForID(var1);
        } while(var6 == null);

        return var6;
    }

    public Iterator<?> getDXFObjectIterator() {
        return new Iterator() {
            int count = 0;

            @Override
            public boolean hasNext() {
                return this.count < DXFDictionary.this.records.size();
            }

            @Override
            public Object next() {
                return ((DXFDictionaryRecord) DXFDictionary.this.records.get(this.count++)).getDXFObject();
            }

            @Override
            public void remove() {
                DXFDictionary.this.records.remove(this.count - 1);
            }
        };
    }

    private static class DXFDictionaryRecord {
        private String id;
        private String name;
        private DXFObject obj;

        public DXFDictionaryRecord(String var2, String var3) {
            this.id = var3;
            this.name = var2;
        }

        public String getName() {
            return this.name;
        }

        public String getID() {
            return this.id;
        }

        public void setID(String var1) {
            this.id = var1;
        }

        public void setDXFObject(DXFObject var1) {
            this.obj = var1;
        }

        public DXFObject getDXFObject() {
            return this.obj;
        }
    }
}
